import android.app.Activity
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

class PrinterPlugin : MethodChannel.MethodCallHandler {
    val mActivity: Activity
    val mChannel: MethodChannel

    companion object {
        val CHANNEL = "cn.sxpei.smartcar/printer"
        fun registerWith(flutterEngine: FlutterEngine, activity: FlutterActivity) {
            var instance = PrinterPlugin(activity, MethodChannel(flutterEngine.dartExecutor, CHANNEL))
            instance.mChannel?.setMethodCallHandler(instance)
        }
    }

    constructor(activity: Activity, channel: MethodChannel) {
        mActivity = activity
        mChannel = channel
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        if (call.method == "add") {
            Toast.makeText(mActivity!!, "123, " + call.argument<Int>("p1"), Toast.LENGTH_LONG).show()
            var p1 = call.argument<Int>("p1")
            var p2 = call.argument<Int>("p2")
            result.success(p1!! + p2!!)
        } else if (call.method == "test2") {
            result.success("123")
        }
    }
}